package com.qxsx.midi.activity.me;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.model.ReceivingAddress;
import com.qxsx.midi.utils.SPUtils;

/**
 * Created by Zrj on 2015/9/14.
 */
public class SystemSetActivity extends BaseActivity implements View.OnClickListener {

    //联系我们,退出登录
    private RelativeLayout rl_receivingaddress, rl_contact, rl_finish, rl_message;


    private TextView tv_exit, tv_cancel;
    String login_name;//用户名

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_set);
        setTitle("系统设置");
        setNavBtn(R.mipmap.back, 0);
        findById();
    }

    @Override
    protected void onResume() {
        super.onResume();
        login_name = (String) SPUtils.get(AppContext.getContext(), "login_name", "");

    }

    private void findById() {
        rl_receivingaddress = (RelativeLayout) findViewById(R.id.rl_receivingaddress);
        rl_receivingaddress.setOnClickListener(this);
        rl_finish = (RelativeLayout) findViewById(R.id.rl_finish);
        rl_finish.setOnClickListener(this);
        rl_contact = (RelativeLayout) findViewById(R.id.rl_contact);
        rl_contact.setOnClickListener(this);
        rl_message = (RelativeLayout) findViewById(R.id.rl_message);
        rl_message.setOnClickListener(this);
    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_receivingaddress:
                if (login_name != "") {
                    startActivity(new Intent(SystemSetActivity.this, MeReceivingAddressActivity.class));
                } else {
                    startActivity(new Intent(SystemSetActivity.this, LoginActivity.class));
                }
                break;
            case R.id.rl_contact:
                if (login_name != "") {
                    startActivity(new Intent(SystemSetActivity.this, ContactUsActivity.class));
                } else {
                    startActivity(new Intent(SystemSetActivity.this, LoginActivity.class));
                }

                break;
            case R.id.rl_finish:
                View view = getLayoutInflater().inflate(R.layout.pop_exit, null);
                tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
                tv_exit = (TextView) view.findViewById(R.id.tv_exit);

                final Dialog dialog = new Dialog(this, R.style.transparentFrameWindowStyle);
                dialog.setContentView(view, new LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
                Window window = dialog.getWindow();
                // 设置显示动画
                window.setWindowAnimations(R.style.main_menu_animstyle);
                WindowManager.LayoutParams wl = window.getAttributes();
                wl.x = 0;
                wl.y = getWindowManager().getDefaultDisplay().getHeight();
                // 以下这两句是为了保证按钮可以水平满屏
                wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
                wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;

                // 设置显示位置
                dialog.onWindowAttributesChanged(wl);
                // 设置点击外围解散
                dialog.setCanceledOnTouchOutside(true);
                dialog.show();
                tv_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        dialog.dismiss();
                    }
                });
                tv_exit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SPUtils.put(AppContext.getContext(), "login_name", "");
                        finish();
                        dialog.dismiss();
                    }
                });
                break;


            case R.id.rl_message:
                if (login_name != "") {
                    startActivity(new Intent(SystemSetActivity.this, InfoSetActivity.class));
                } else {
                    startActivity(new Intent(SystemSetActivity.this, LoginActivity.class));
                }

                break;

        }

    }
}


